package cn.airfei.netty.nettyexample.rpc.netty;

import cn.airfei.netty.nettyexample.rpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @description:
 * @author: air
 * @create: 2021-01-22 15:33
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 获取客户端发送的消息
        System.out.println("msg="+msg);
        // 满足自定义协议
        if (msg.toString().startsWith("HelloService#")){
            new HelloServiceImpl().hello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));

        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
